package com.eqdd.yiqidian.ui;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.View;

import com.eqdd.inputs.AndroidNextInputs;
import com.eqdd.inputs.WidgetAccess;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.nextinputs.ValueScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RegistTwoCustom;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.EncryptUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.web.WebActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.HashMap;

public class RegistTwo extends BaseActivity {
    private RegistTwoCustom dataBinding;
    private String phone;
    private AndroidNextInputs inputs;
    private WidgetAccess access;
    private Bundle bundle;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_regist_two);
        initTopTitleBar(View.VISIBLE, "个人注册");
    }

    @Override
    public void initData() {
        phone = getIntent().getStringExtra("phone");
        inputs = new AndroidNextInputs();
        access = new WidgetAccess(this);
        inputs  // 必选，密码
                .add(access.findEditText(R.id.et_reg_username))
                .with(StaticScheme.Required(), ValueScheme.RangeLength(8, 18));
    }

    @Override
    public void setView() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_regeist:
                if (inputs.test()) {
                    registNormal(dataBinding.etRegUsername.getText().toString());
                } else {
                    ToastUtil.showShort("密码格式有误");
                }
                break;
            case R.id.tv_fuwuxieyi:
                bundle = new Bundle();
                bundle.putString(Config.TITLE, "易企点服务使用协议");
                bundle.putString(Config.URL,HttpConfig.FUWUSHIYONG);
                startActivity(WebActivity.class);
                break;
            case R.id.tv_yinsi:
                bundle = new Bundle();
                bundle.putString(Config.TITLE, "隐私条款");
                bundle.putString(Config.URL,HttpConfig.YINSITIAOKUAN);
                startActivity(WebActivity.class);
        }
    }

    public void registNormal(String pass) {
        HashMap<String, String> map = new HashMap<>();
        map.put("u1", phone);
        map.put("password", EncryptUtil.sha1(pass + "EQD"));
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.REGISTER)
                .params(map)
                .execute(new DialogCallBack<HttpResult>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            finish();
                        }
                    }
                });
    }
}
